
using Abp.Domain.Repositories;
using Abp.EntityFrameworkCore.Repositories;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.DomainEntities.Parameters.DomainService;
using SOEI.Solution.Helper;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.Integratedes.Organization.DomainService
{
    /// <summary>
    /// 领域服务层一个模块的核心业务逻辑
    ///</summary>
    public class OEconomyPersonChangeManager : DomainServiceBase, IOEconomyPersonChangeManager
    {

        private readonly IRepository<OEconomyPersonChange, string> _oeconomypersonchangeRepository;

        /// <summary>
        /// 【Parameter】参数的领域服务
        /// </summary>
        private readonly IParameterManager _parameterManager;

        /// <summary>
        /// 【OEconomyPersonChange】的构造方法
        /// 通过构造函数注册服务到依赖注入容器中
        ///</summary>
        public OEconomyPersonChangeManager(
            IRepository<OEconomyPersonChange, string> oeconomypersonchangeRepository,
            IParameterManager parameterManager
        )
        {
            _oeconomypersonchangeRepository = oeconomypersonchangeRepository;
            _parameterManager = parameterManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        /// 返回列表查询用
        /// </summary>
        /// <returns></returns>
        public IQueryable<OEconomyPersonChange> QueryEntityListAsNoTracking()
        {

            var query = _oeconomypersonchangeRepository.GetAll().AsNoTracking()
                        .Select(x => new OEconomyPersonChange
                        {

                            Id = x.Id,
                            ZZBGSQBH = x.ZZBGSQBH,
                            RYBH = x.RYBH,
                            RYMC = x.RYMC,
                            RYLX = x.RYLX,
                            XB = x.XB,
                            CSNY = x.CSNY,
                            JG = x.JG,
                            JTZZ = x.JTZZ,
                            SFZH = x.SFZH,
                            ZW = x.ZW,
                            ZGZH = x.ZGZH,
                            ZGZFZDW = x.ZGZFZDW,
                            SGZH = x.SGZH,
                            SGZFZDW = x.SGZFZDW,
                            JYTS = x.JYTS,
                            JYMJ = x.JYMJ,
                            LXDH = x.LXDH,
                            ZP = x.ZP,
                            ZYPXJL = x.ZYPXJL,
                            GZJL = x.GZJL,
                            JGBH = x.JGBH,
                            FZJGBH = x.FZJGBH,
                            XLDM = x.XLDM,
                            MZDM = x.MZDM,
                            JSZCDM = x.JSZCDM,
                            ZZMMDM = x.ZZMMDM,
                            RYZT = x.RYZT,
                            MM = x.MM,
                            LRSJ = x.LRSJ,
                            XLZH = x.XLZH,
                            XLZCZH = x.XLZCZH,
                            ZGZCZH = x.ZGZCZH,
                            SGZYXQXQ = x.SGZYXQXQ,
                            SGZYXQXZ = x.SGZYXQXZ,
                            EKEYSN = x.EKEYSN,
                            SPZT = x.SPZT,
                            AJBH = x.AJBH,
                            GZGW = x.GZGW,
                            AJSPZT = x.AJSPZT,
                            ZXSJ = x.ZXSJ,
                            ZXRBH = x.ZXRBH,
                            ZXYY = x.ZXYY,
                            KWQ = x.KWQ,
                            BGRBH = x.BGRBH,
                            BGRMC = x.BGRMC,
                            BGSJ = x.BGSJ,
                            ZJLXDM = x.ZJLXDM,
                            RYLXMC = x.RYLXMC,
                        });
            return query;
        }

        /// <summary>
        /// 【OEconomyPersonChange】返回表达式数的实体信息即IQueryable类型
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<OEconomyPersonChange> Query()
        {
            return _oeconomypersonchangeRepository.GetAll();
        }
        /// <summary>
        /// 【OEconomyPersonChange】返回即IQueryable类型的实体，不包含EF Core跟踪标记
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<OEconomyPersonChange> QueryAsNoTracking()
        {
            return _oeconomypersonchangeRepository.GetAll().AsNoTracking();
        }
        /// <summary>
        /// 【OEconomyPersonChange】根据Id查询实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>实体</returns>
        public async Task<OEconomyPersonChange> FindByIdAsync(string id)
        {
            var entity = await _oeconomypersonchangeRepository.GetAsync(id);
            return entity;
        }
        /// <summary>
        /// 【OEconomyPersonChange】检查实体是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns>bool</returns>
        public async Task<bool> IsExistAsync(string id)
        {
            var result = await _oeconomypersonchangeRepository.GetAll().AnyAsync(a => a.Id == id);
            return result;
        }
        /// <summary>
        /// 【OEconomyPersonChange】创建实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<OEconomyPersonChange> CreateAsync(OEconomyPersonChange entity)
        {
            entity.Id = await _oeconomypersonchangeRepository.InsertAndGetIdAsync(entity);
            return entity;
        }
        /// <summary>
        /// 【OEconomyPersonChange】更新实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task UpdateAsync(OEconomyPersonChange entity)
        {
            await _oeconomypersonchangeRepository.UpdateAsync(entity);
        }
        /// <summary>
        /// 【OEconomyPersonChange】删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(string id)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _oeconomypersonchangeRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 【OEconomyPersonChange】批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task BatchDelete(List<string> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _oeconomypersonchangeRepository.DeleteAsync(a => input.Contains(a.Id));
        }
        #endregion


        #region -------------------------------------------------用户自定义------------------------------------------------
        // <summary>
        /// 【原始系统】获取机构人员编号
        /// </summary>
        /// <returns></returns>
        public object GetSequence()
        {
            var sql = string.Format("select {0}.Nextval from dual", "SEQ_CLFWQ_RYBGSQXX_RYBGSQBH");
            return _oeconomypersonchangeRepository.GetDbContext().Database.ExecuteScalar(sql);
        }

        /// <summary>
        /// 【原始系统】获取新的人员编号
        /// </summary>
        /// <returns></returns>
        public string GetNewSequenceCode()
        {
            var code = this.GetSequence().ToString();
            return code.PadLeft(10, '0');
        }

        /// <summary>
        /// 通过关联查询获取列表查询结果
        /// </summary>
        /// <returns></returns>
        public IQueryable<OEconomyPersonChange> QueryUnionEntityListAsNoTracking()
        {
            // 这里目前必须要把类型查出来然后在联合查询

            var ListEntity = this._parameterManager.QueryPersonType().ToList();
            var CaseStatusList = this._parameterManager.QueryCaseStatus().ToList();
            var query = this.QueryEntityListAsNoTracking();
            
            var result = from x in query
                         select new OEconomyPersonChange
                         {
                             Id = x.Id,
                             ZZBGSQBH = x.ZZBGSQBH,
                             RYBH = x.RYBH,
                             RYMC = x.RYMC,
                             RYLX = x.RYLX,
                             XB = x.XB,
                             CSNY = x.CSNY,
                             JG = x.JG,
                             JTZZ = x.JTZZ,
                             SFZH = x.SFZH,
                             ZW = x.ZW,
                             ZGZH = x.ZGZH,
                             ZGZFZDW = x.ZGZFZDW,
                             SGZH = x.SGZH,
                             SGZFZDW = x.SGZFZDW,
                             JYTS = x.JYTS,
                             JYMJ = x.JYMJ,
                             LXDH = x.LXDH,
                             ZP = x.ZP,
                             ZYPXJL = x.ZYPXJL,
                             GZJL = x.GZJL,
                             JGBH = x.JGBH,
                             FZJGBH = x.FZJGBH,
                             XLDM = x.XLDM,
                             MZDM = x.MZDM,
                             JSZCDM = x.JSZCDM,
                             ZZMMDM = x.ZZMMDM,
                             RYZT = x.RYZT,
                             MM = x.MM,
                             LRSJ = x.LRSJ,
                             XLZH = x.XLZH,
                             XLZCZH = x.XLZCZH,
                             ZGZCZH = x.ZGZCZH,
                             SGZYXQXQ = x.SGZYXQXQ,
                             SGZYXQXZ = x.SGZYXQXZ,
                             EKEYSN = x.EKEYSN,
                             SPZT = x.SPZT,
                             AJBH = x.AJBH,
                             GZGW = x.GZGW,
                             AJSPZT = x.AJSPZT,
                             ZXSJ = x.ZXSJ,
                             ZXRBH = x.ZXRBH,
                             ZXYY = x.ZXYY,
                             KWQ = x.KWQ,
                             BGRBH = x.BGRBH,
                             BGRMC = x.BGRMC,
                             BGSJ = x.BGSJ,
                             ZJLXDM = x.ZJLXDM,
                             // 联合查询属性
                             RYLXMC   = EntityHelper.GetEntityParameter(ListEntity, x.RYLX).Value,
                             AJSPZTMC = EntityHelper.GetEntityParameter(CaseStatusList, x.AJSPZT).Value
                         };
            return result;
        }



        #endregion









    }
}
